<template>
  <div class="app-container">
    <el-form>
      <el-form-item>
        <el-tooltip effect="light" content="同步最新结果" placement="right-end">
          <el-button circle icon="el-icon-refresh" size="mini" @click="handleRefresh" />
        </el-tooltip>
        <el-tooltip effect="light" content="导出" placement="right-end">
          <el-button circle icon="el-icon-download" size="mini" @click="handleExport" />
        </el-tooltip>
        <el-button
          v-if="queryParams.taskId && checkRole(['admin'])"
          type="text"
          @click="handleMarkerReport(queryParams.taskId)"
          >显示标注员</el-button
        >
      </el-form-item>
    </el-form>
    <div style="font-size: 12px; color: gray">{{ taskPath }}</div>

    <el-table v-loading="loading" :data="reportList">
      <el-table-column label="任务名" prop="taskName" />
      <el-table-column v-if="rectTotalCount > 0" label="二维矩形框" align="center" prop="rectCount" />
      <el-table-column v-if="boxTotalCount > 0" label="二维立体框" align="center" prop="boxCount" />
      <el-table-column v-if="lineTotalCount > 0" label="二维折线" align="center" prop="lineCount" />
      <el-table-column v-if="polyTotalCount > 0" label="二维分割" align="center" prop="polyCount" />
      <el-table-column v-if="point1TotalCount > 0" label="二维单点" align="center" prop="point1Count" />
      <el-table-column v-if="rectPoint1TotalCount > 0" label="二维框带点" align="center" prop="rectPoint1Count" />
      <el-table-column v-if="cubeTotalCount > 0" label="三维立体框" align="center" prop="cubeCount" />
      <el-table-column v-if="splitTotalCount > 0" label="三维分割" align="center" prop="splitCount" />
      <el-table-column v-if="otherTotalCount > 0" label="其他类型" align="center" prop="otherCount" />
      <el-table-column label="更新时间" prop="refreshTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.refreshTime) }}</span>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { listReport, exportReport, refreshReport } from '@/api/mark/report'
import { getQaTask } from '@/api/mark/qaTask'
import { checkRole } from '@/utils/permission'

export default {
  name: 'Report',
  // components: { Treeselect },
  data() {
    return {
      // 验收任务树选项
      qaTaskOptions: [],
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 标注框数统计报表格数据
      reportList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 50,
        taskId: undefined,
        taskName: undefined,
      },
      rectTotalCount: 0,
      boxTotalCount: 0,
      lineTotalCount: 0,
      polyTotalCount: 0,
      point1TotalCount: 0,
      rectPoint1TotalCount: 0,
      cubeTotalCount: 0,
      splitTotalCount: 0,
      otherTotalCount: 0,
      taskPath: '',
    }
  },
  created() {
    if (this.$route.query.taskId) {
      this.queryParams = {
        taskId: this.$route.query.taskId,
      }
      this.getList()
    }
  },
  methods: {
    checkRole,
    /** 转换验收任务数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children
      }
      return {
        id: node.taskId,
        label: node.taskName,
        children: node.children,
      }
    },
    /** 查询标注框数统计报列表 */
    getList() {
      this.taskPath = ''
      if (this.$route.query.taskId && this.$route.query.taskId !== 0) {
        getQaTask(this.$route.query.taskId).then((response) => {
          if (response.data) {
            this.taskPath = response.data.taskName
            this.markType = response.data.markType
            if (response.data.parentId !== 0) {
              getQaTask(response.data.parentId).then((response) => {
                this.taskPath = response.data.taskName + '@' + this.taskPath
                if (response.data.parentId !== 0) {
                  getQaTask(response.data.parentId).then((response) => {
                    this.taskPath = response.data.taskName + '@' + this.taskPath
                  })
                }
              })
            }
          }
        })
      }
      if (this.queryParams.taskId) {
        this.loading = true
        listReport(this.queryParams).then((response) => {
          this.rectTotalCount = 0
          this.boxTotalCount = 0
          this.lineTotalCount = 0
          this.polyTotalCount = 0
          this.point1TotalCount = 0
          this.rectPoint1TotalCount = 0
          this.cubeTotalCount = 0
          this.splitTotalCount = 0
          this.otherTotalCount = 0
          this.reportList = response.rows
          this.reportList.forEach((row) => {
            this.rectTotalCount += row.rectCount
            this.boxTotalCount += row.boxCount
            this.lineTotalCount += row.lineCount
            this.polyTotalCount += row.polyCount
            this.point1TotalCount += row.point1Count
            this.rectPoint1TotalCount += row.rectPoint1Count
            this.cubeTotalCount += row.cubeCount
            this.splitTotalCount += row.splitCount
            this.otherTotalCount += row.otherCount
          })
          this.total = response.total
          this.loading = false
        })
      } else {
        alert('请选择上级任务或输入任务名称')
      }
    },
    /** 搜索按钮操作 */
    // handleQuery() {
    //   this.queryParams.pageNum = 1
    //   this.getList()
    // },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.taskId)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams
      this.$confirm('是否确认导出所有标注框数统计报数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(function () {
          return exportReport(queryParams)
        })
        .then((response) => {
          this.download(response.msg)
        })
        .catch(function () {})
    },
    handleRefresh() {
      refreshReport(this.queryParams).then((response) => {
        if (response.code == 200) {
          this.msgSuccess('刷新成功！')
          this.rectTotalCount = 0
          this.boxTotalCount = 0
          this.lineTotalCount = 0
          this.polyTotalCount = 0
          this.point1TotalCount = 0
          this.rectPoint1TotalCount = 0
          this.cubeTotalCount = 0
          this.splitTotalCount = 0
          this.otherTotalCount = 0
          this.reportList = response.rows
          this.reportList.forEach((row) => {
            this.rectTotalCount += row.rectCount
            this.boxTotalCount += row.boxCount
            this.lineTotalCount += row.lineCount
            this.polyTotalCount += row.polyCount
            this.point1TotalCount += row.point1Count
            this.rectPoint1TotalCount += row.rectPoint1Count
            this.cubeTotalCount += row.cubeCount
            this.splitTotalCount += row.splitCount
            this.otherTotalCount += row.otherCount
          })
          this.total = response.total
          this.loading = false
        }
      })
    },
    handleMarkerReport(taskId) {
      this.$router.push({ path: '/mark/markFactoryReport', query: { taskId: taskId, pageNum: 1 } })
    },
  },
}
</script>
